package com.qjcc.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.qjcc.R;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.annotations.res.StringRes;

@EBean
public class LocationUtil {
    @RootContext
    Context context;
    @StringRes(resName = "location_unusual_tip")
    String location_unusual_tip;
    @StringRes(resName = "relocation_tip")
    String relocation_tip;
    @StringRes(resName = "no_rule_location_tip")
    String no_rule_location_tip;
    private PopupWindow popupWindow;
    LocationClient mLocationClient;
    LocationClientOption.LocationMode tempMode = LocationClientOption.LocationMode.Hight_Accuracy;
    String tempcoor="";//"gcj02";
    boolean isLocationSucess = false;
    double curLngAute = 0;
    double curLangAute = 0;
    String curtitle ="";

    public LocationUtil(Context context){
        this.context = context;
    }
    /*public void location(final Done done){
//        synchronized(this) {
            String tempModeString = context.getString(R.string.tempMode);
            if (tempModeString.equals("Hight_Accuracy")) {
                tempMode = LocationClientOption.LocationMode.Hight_Accuracy;
            } else if (tempModeString.equals("Battery_Saving")) {
                tempMode = LocationClientOption.LocationMode.Battery_Saving;
            } else if (tempModeString.equals("Device_Sensors")) {
                tempMode = LocationClientOption.LocationMode.Device_Sensors;
            }
            tempcoor = context.getString(R.string.tempcoor);
            // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
            SDKInitializer.initialize(context.getApplicationContext());
            mLocationClient = new LocationClient(context.getApplicationContext());
            initLocation();
            mLocationClient.start();
            // 注册位置监听器
            mLocationClient.registerLocationListener(new BDLocationListener() {
                @Override
                public void onReceiveLocation(BDLocation location) {
                    try {
                        done.callback(location);
                        mLocationClient.stop();
                    } catch (Exception e) {
                        try {
                            mLocationClient.stop();
                        } catch (Exception e2) {
                            Log.e("===", "", e2);
                        }
                    }
                }
            });
//        }
    }

    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(tempMode);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系，
        int span=1000;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIgnoreKillProcess(true);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setAddrType("all");
        option.disableCache(true);//禁止启用缓存定位
        mLocationClient.setLocOption(option);
    }

    public void showLocation(AttendancePunchDetail attendancePunchDetail){
        Intent intent = new Intent();
        intent.putExtra("latitude", attendancePunchDetail.getPunchLatitude());//40.056858;//
        intent.putExtra("longitude", attendancePunchDetail.getPunchLongitude());//116.308194;//
        intent.putExtra("title", attendancePunchDetail.getPunchAddress());
        intent.putExtra("decribleTitle", "");
        intent.setClass(context, LocationMapActivity.class);
        context.startActivity(intent);
    }

    public void errorTip(final Fragment fragment,AttendancePunchDetail attendancePunchDetail, final ComDone relocattion, final ComDone punch,final ComDone exit){
        View contentView = LayoutInflater.from(context).inflate( R.layout.dialog_reflocation, null);
        TextView curLocation = (TextView) contentView.findViewById(R.id.cur_location);
        CharSequence curTip = curLocation.getText();
        String ruleAddress = attendancePunchDetail != null && attendancePunchDetail.getRuleAddress() !=null ?attendancePunchDetail.getRuleAddress():null;
        String punchAddress =attendancePunchDetail != null && attendancePunchDetail.getPunchAddress() !=null ?attendancePunchDetail.getPunchAddress():null;
        String titleTemp = punchAddress == null ? "未获取到定位地址" : punchAddress;
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(curTip+titleTemp);
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.rgb(244, 121, 123)), curTip.length(), curTip.length() + titleTemp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        curLocation.setText(spannableStringBuilder);
        if(ruleAddress != null ){
            ((TextView)contentView.findViewById(R.id.err_tip)).setText(relocation_tip);
        }else{
            ((TextView)contentView.findViewById(R.id.err_tip)).setText(no_rule_location_tip);
        }
        contentView.findViewById(R.id.btn_relocate).setOnClickListener(new View.OnClickListener() {//重定位11
            @Override
            public void onClick(View v) {
                if(relocattion != null){
                    relocattion.callback();
                }
            }
        });
        contentView.findViewById(R.id.btn_punch).setOnClickListener(new View.OnClickListener() {//打卡12
            @Override
            public void onClick(View v) {
                if(punch != null){
                    punch.callback();
                }
            }

        });
        contentView.findViewById(R.id.btn_exit).setOnClickListener(new View.OnClickListener() {//放弃操作10
            @Override
            public void onClick(View v) {
                if(exit != null){
                    exit.callback();
                }
                if(popupWindow != null){
                    popupWindow.dismiss();
                }
            }
        });

        popupWindow = popupWindow != null ? popupWindow : new PopupWindow(contentView,RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT, true);
        popupWindow.showAtLocation(fragment.getView(),Gravity.CENTER,0,0);
        popupWindow.setWindowLayoutMode(0, 0);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        popupWindow.showAsDropDown(fragment.getView());
    }

    public void closePupwindow(){
        this.popupWindow.dismiss();
    }

    public interface Done{//定位回调接口
        void callback(BDLocation location);
    }

    public interface ComDone{//地点异常显示回调接口
        void callback();
    }
    */
}
